package team.szm.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import team.szm.scd.database.entity.Topic;
import team.szm.scd.database.entity.TopicReply;
import team.szm.scd.database.mapper.TopicMapper;
import team.szm.scd.database.mapper.TopicReplyMapper;
import team.szm.scd.service.interfaces.ITopicService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import static team.szm.scd.config.NormalConfig.TopicPageSize;

@Service
public abstract class AbstractTopicService implements ITopicService {
    @Resource
    TopicMapper topicMapper;
    @Resource
    TopicReplyMapper replyMapper;

    @Override
    public Topic fromId(String id) {
        return topicMapper.selectById(id);
    }

    @Override
    public boolean createTopic(Topic topic, LocalDateTime now) {
        topic.setCreateTime(now);
        return topicMapper.insert(topic) >= 1;
    }

    @Override
    public boolean replyTopic(TopicReply reply, LocalDateTime now) {
        reply.setCreateTime(now);
        return replyMapper.insert(reply) >= 1;
    }

    @Override
    public Page<Topic> getAllTopics(int pageNum, String searchInfo) {
        Page<Topic> page = new Page<>(pageNum, TopicPageSize);
        QueryWrapper<Topic> wrapper = new QueryWrapper<Topic>()
                .like("title", searchInfo)
                .or()
                .like("content", searchInfo);
        return topicMapper.selectPage(page, wrapper);
    }

    @Override
    public Page<TopicReply> getAllReplies(int pageNum, String topicId) {
        Page<TopicReply> page = new Page<>(pageNum, TopicPageSize);
        QueryWrapper<TopicReply> wrapper = new QueryWrapper<>();
        wrapper.eq("tid", topicId);
        return replyMapper.selectPage(page, wrapper);
    }
}
